<template>
  <el-dialog class="gl_dialog_TitleLine" title="上传证明" :visible.sync="isShow" :close-on-click-modal="false" width="50%">
    <el-scrollbar v-loading="dialogLoading" wrap-class="scrollbar-wrapper gl_dialog_height">
      <div style="text-align">
        <div v-for="(item, index) in fileList" :key="index" class="fileBox">
          <upload-file
            ref="uploadFile"
            :action="action"
            :file-item="item"
            file-type="file"
            format-file=".pdf,.jpg,.png"
            default-file-name="体检资料"
            :default-file-url="getStaticFileUrl(fileList[index].fileDir)"
            @uploadSuccess="photoSave"
            @deleteFile="deleteFile"
          />
        </div>
      </div>
    </el-scrollbar>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" type="primary" @click="close">{{ $t('button.submit') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { deepCopy } from '@/lib';
import { translateByCode } from '@/lib/getTransData';
import { GameSignAddMember_Rules } from '@/lib/validate/execte';
import uploadFile from './uploadFileDialog';
import {
  api_POST_EventsManagementComRegisterFile, // 新建注册带标签
  api_GET_EventsManagementComRegisterFileCompetitionEntryFileByRegisterCompetitorRegisterId, // 8-查询体检资料
  api_DELETE_EventsManagementComRegisterFileId // 10-删除体检资料
} from '@/extend/api/gameswaggerApi/app';
import {
  SiSTKWEventsManagementCreateCompetitionRegisterFileInput
} from '@/lib/form/game/swaggerForm';
import {
  FileInputFrom
} from '@/lib/form/home/enter/member';
export default {
  components: {
    uploadFile
  },
  data () {
    return {
      action: `${WEB_CONFIG.VUE_APP_STATIC_FILE_RUL}/api/upload/image`,
      isShow: false,
      dialogLoading: false,
      files: 1, // 文件的数量
      RegisterFileInput: new SiSTKWEventsManagementCreateCompetitionRegisterFileInput(),
      FileInputFrom: new FileInputFrom(),
      rules: new GameSignAddMember_Rules(),
      fileList: [],
      personData: {
        competitorRegisterId: '',
        competitionId: ''
      }
    };
  },
  methods: {
    open (row, gameId) {
      this.personData.competitionId = gameId;
      this.personData.competitorRegisterId = row.id;
      this.RegisterFileInput = new SiSTKWEventsManagementCreateCompetitionRegisterFileInput();
      this.api_GET_EventsManagementComRegisterFileCompetitionEntryFileByRegisterCompetitorRegisterId();
      this.isShow = true;
      this.dialogLoading = true;
    },
    // 查询体检资料
    api_GET_EventsManagementComRegisterFileCompetitionEntryFileByRegisterCompetitorRegisterId() {
      this.FileInputFrom = new FileInputFrom();
      api_GET_EventsManagementComRegisterFileCompetitionEntryFileByRegisterCompetitorRegisterId(this.personData.competitorRegisterId).then(res => {
        console.log('查询体检资料', res);
        this.fileList = res;
        this.fileList = deepCopy(res);
        this.fileList.push(this.FileInputFrom);
        this.dialogLoading = false;
      }, () => {
        this.fileList = [];
        this.fileList.push(this.FileInputFrom);
        this.$message({ type: 'error', message: '获取体检资料失败' });
        this.dialogLoading = false;
      });
    },
    // 回显
    photoSave(file) {
      this.RegisterFileInput = new SiSTKWEventsManagementCreateCompetitionRegisterFileInput();
      this.RegisterFileInput.competitionId = this.personData.competitionId;
      this.RegisterFileInput.competitorRegisterId = this.personData.competitorRegisterId;
      this.RegisterFileInput.fileDir = file.fileUrl.tempUrl;
      api_POST_EventsManagementComRegisterFile(this.RegisterFileInput).then(res => {
        this.$message({
          // 上传成功！
          message: '上传成功！',
          type: 'success'
        });
        this.api_GET_EventsManagementComRegisterFileCompetitionEntryFileByRegisterCompetitorRegisterId();
      })
        .catch(() => {
        });
    },
    // 删除文件
    deleteFile(id) {
      console.log(id);
      // 是否删除改文件
      const _confirmStr = '是否删除改文件';
      this.$confirm(_confirmStr, translateByCode('alert.warning'), {
        confirmButtonText: translateByCode('alert.confirm'),
        cancelButtonText: translateByCode('alert.cancel'),
        type: 'warning'
      }).then(() => {
        api_DELETE_EventsManagementComRegisterFileId(id).then(res => {
          this.$message({
            type: 'success',
            // 删除成功！
            message: translateByCode('alert.deleteSuccessfully')
          });
          this.api_GET_EventsManagementComRegisterFileCompetitionEntryFileByRegisterCompetitorRegisterId();
        })
          .catch(() => {
          });
      }).catch(() => {
      });
    },
    // 获取静态文件
    getStaticFileUrl(relativeUrl) {
      if (!relativeUrl) return '';
      return WEB_CONFIG.VUE_APP_STATIC_FILE_RUL + relativeUrl;
    },
    close () {
      this.isShow = false;
    },
    submitSelectTable () {
      this.$emit('onSubmitAddMember', Object.assign({}, this.RegisterFileInput));
      this.close();
    }
  }
};
</script>
<style scoped>
  .fileBox{
    display: inline-block;
    vertical-align: middle;
    margin: 0 10px;
  }
  .upload_game_img {
    width: 200px;
    height: 200px;
    line-height: 200px;
    text-align: center;
    font-size: 20px;
    border: 1px dotted #DCDFE6;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
  }
  .upload_game_img:hover {
    border-color: #409eff;
  }
  .upload_game_img .show_img {
    max-width: 100%;
    max-height: 100%;
    border-radius: 4px;
  }
</style>
